<?php
class Champs {
	
	private $donnees=array(
					"nom" => NULL,
					"valeur" => NULL,
					"type" => NULL,
					"modifie" => false,
					"cle" => false,
					"recherche" => false,
					"libelle" => NULL,
					"affichable" => true,
					"modifiable" => true,
					"cleEtrangere" => NULL,
					"password" => false
	);
		
	function __construct($nom=NULL, $valeur=NULL, $type=NULL,$libelle=NULL,
			$cle=false,$rechercher=false,$affichable=true,$modifiable=true,
			$password=false) {
		if(strlen($nom)>0) {
			$this->setnom($nom);
			if(!is_null($valeur)) {
				switch ($type) {
					case "nombre": $this->setnombre($valeur); break;
					case "booleen": $this->setbooleen($valeur); break;
					case "chaine": $this->setchaine($valeur); break;
					case "date": $this->setdate($valeur); break;
					default: $this->setvaleur($valeur); break;
				}
			}
		}
		$this->setcle($cle);
		$this->setrecherche($rechercher);
		$this->setlibelle($libelle);
		$this->setaffichable($affichable);
		$this->setmodifiable($modifiable);
		$this->setpassword($password);
	}
	
	public function setcleEtrangere($param) {
		$this->donnees["cleEtrangere"]=$param;
	}
	
	public function iscleEtrangere() {
		if ($this->donnees["cleEtrangere"] == NULL) return false;
		else return true;
	}
	
	public function getcleEtrangere() {
		return $this->donnees["cleEtrangere"];
	}
	
	public function setnom($nom) {
		$this->donnees["nom"]=$nom;
		$this->setmodifie(true);
	}
	
	public function getnom() {
		return $this->donnees["nom"];
	}
	
	public function setchaine($s) {
		$this->setvaleur($s);
		$this->settype("chaine");
	}
	
	public function setnombre($n) {
		$this->setvaleur($n);
		$this->settype("nombre");
	}

	public function setdate($d) {
		$this->settype("date");
		if (is_object($d) && $d instanceof DateTime)
			$this->setvaleur($d);
		else if (is_string($d) && strlen($d)>0) {
			$dt=false;
			$elements=preg_split("/[\/\s\:]+/",$d);
			if(count($elements)==5 && checkdate($elements[1], $elements[0], $elements[2])) {
				$dt=new DateTime('now',new DateTimeZone("UTC"));
				$dt->setDate($elements[2],$elements[1], $elements[0]);
				$dt->setTime($elements[3], $elements[4]);
			}
			if(!$dt && count($elements)==3 && checkdate($elements[1], $elements[0], $elements[2])) {
				$dt=new DateTime('now',new DateTimeZone("UTC"));
				$dt->setDate($elements[2],$elements[1], $elements[0]);
				$dt->setTime('0', '0');
			}
			if(!$dt)
				$dt=new DateTime($d,new DateTimeZone("UTC"));
			if($dt)
				$this->setvaleur($dt);
			else {
				$this->setvaleur(NULL);
				return false;
			}
		} else {
			$this->setvaleur(NULL);			
			return false;
		}
	}
	
	public function setbooleen($b) {
		if($b==true || $b==1 || !strcasecmp($b, 'true') || !strcasecmp($b, 'yes')
			|| !strcasecmp($b,'vrai') || !strcasecmp($b,'oui'))
			$this->setvaleur(true);
		else if ($b==false || $b==0 || !strcasecmp($b, 'false') || !strcasecmp($b, 'no')
			|| !strcasecmp($b,'faux') || !strcasecmp($b,'non'))
			$this->setvaleur(false);
		else return false;
		$this->settype("booleen");
		return true;
	}
	
	public function setvaleur($param) {
		$this->donnees["valeur"]=$param;
		$this->setmodifie(TRUE);
	}
	
	public function getvaleur() {
		return $this->donnees["valeur"];
	}
	
	public function setmodifie($param) {
		$this->donnees["modifie"]=$param;
	}
	
	public function ismodifie() {
		return $this->donnees["modifie"];
	}
	
	public function setcle($valeur) {
		$this->donnees["cle"]=$valeur;
		$this->setmodifie(true);
	}
	
	public function iscle() {
		return $this->donnees["cle"];
	}
	
	public function settype($param) {
		$this->donnees["type"]=$param;
		$this->setmodifie(true);
	}
	
	public function gettype() {
		return $this->donnees["type"];
	}
	
	public function setrecherche($valeur) {
		$this->donnees["recherche"]=$valeur;
	}
	
	public function isrecherche() {
		return $this->donnees["recherche"];
	}
	
	public function setlibelle($libelle) {
		$this->donnees["libelle"]=$libelle;
	}
	
	public function getlibelle() {
		return $this->donnees["libelle"];
	}
	
	public function isnull() {
		$v=$this->donnees['valeur'];
		if ($v===NULL || (is_string($v) && strlen($v)==0))
			return true;
		else
			return false;
	}
	
	public function setaffichable($a) {
		$this->donnees['affichable']=$a;
	}
	
	public function isaffichable() {
		return $this->donnees['affichable'];
	}
	
	public function setmodifiable($m) {
		$this->donnees['modifiable']=$m;
	}
	
	public function ismodifiable() {
		return $this->donnees['modifiable'];
	}
	
	public function afficher() {
		if($this->isnull()) return '';
		$valeur=$this->donnees['valeur'];
		switch ($this->donnees['type']) {
			case 'nombre': case 'chaine': return $valeur; break;
			case 'date': if($valeur->format('H')=='0') return $valeur->format('d/m/Y');
						 else return $valeur->format('d/m/Y H:i'); break;
			case 'booleen': if ($valeur==false) return 'Non';
							else return 'Oui';
		}
	}
	
	public function valeurSQL() {
		if($this->isnull())
			return 'NULL';
		$valeur=$this->donnees['valeur'];
		switch ($this->donnees['type']) {
			case 'nombre': return $valeur; break;
			case 'chaine': return "'".$valeur."'"; break;
			case 'date': if($valeur->format('H')=='0') return "'".$valeur->format('Y-m-d')."'";
						 else return "'".$valeur->format('Y-m-d H:i:s')."'";break;
			case 'booleen': if ($valeur==false) return 'false';
								else return 'true';
							break;
		}
	}
	
	public function setpassword($p=true) {
		$this->donnees['password']=$p;
	}
	
	public function ispassword() {
		return $this->donnees['password'];
	}
}
?>